<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页统计接口测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .api-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background: #fafafa;
        }
        .api-section h3 {
            color: #666;
            margin-top: 0;
        }
        .btn {
            padding: 10px 20px;
            margin: 5px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        .btn-info {
            background-color: #17a2b8;
            color: white;
        }
        .btn-warning {
            background-color: #ffc107;
            color: black;
        }
        .result {
            margin-top: 15px;
            padding: 15px;
            border-radius: 4px;
            background: #f8f9fa;
            border-left: 4px solid #007bff;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
            max-height: 300px;
            overflow-y: auto;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
            color: #721c24;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }
        .stat-card {
            background: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            text-align: center;
        }
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #007bff;
        }
        .stat-label {
            color: #666;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>首页统计接口测试</h1>

        <!-- 基础统计数据 -->
        <div class="api-section">
            <h3>1. 基础统计数据</h3>
            <button class="btn btn-primary" onclick="getStats()">获取统计数据</button>
            <div id="statsResult" class="result" style="display: none;"></div>
            <div id="statsCards" class="stats-grid"></div>
        </div>

        <!-- 课程分类统计 -->
        <div class="api-section">
            <h3>2. 课程分类统计</h3>
            <button class="btn btn-success" onclick="getCategoryStats()">获取分类统计</button>
            <div id="categoryResult" class="result" style="display: none;"></div>
        </div>

        <!-- 最近7天统计 -->
        <div class="api-section">
            <h3>3. 最近7天统计</h3>
            <button class="btn btn-info" onclick="getRecentStats()">获取最近7天统计</button>
            <div id="recentResult" class="result" style="display: none;"></div>
        </div>

        <!-- 教师状态统计 -->
        <div class="api-section">
            <h3>4. 教师状态统计</h3>
            <button class="btn btn-warning" onclick="getTeacherStatusStats()">获取教师状态统计</button>
            <div id="teacherStatusResult" class="result" style="display: none;"></div>
        </div>

        <!-- 课程状态统计 -->
        <div class="api-section">
            <h3>5. 课程状态统计</h3>
            <button class="btn btn-warning" onclick="getCourseStatusStats()">获取课程状态统计</button>
            <div id="courseStatusResult" class="result" style="display: none;"></div>
        </div>

        <!-- 全部测试 -->
        <div class="api-section">
            <h3>6. 全部测试</h3>
            <button class="btn btn-primary" onclick="testAll()">测试所有接口</button>
            <div id="allTestResult" class="result" style="display: none;"></div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8082';

        // 显示结果
        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.style.display = 'block';
            element.className = isError ? 'result error' : 'result';
            element.textContent = JSON.stringify(data, null, 2);
        }

        // 1. 获取基础统计数据
        async function getStats() {
            try {
                const response = await fetch(`${API_BASE}/dashboard/stats`);
                const data = await response.json();
                showResult('statsResult', data);
                
                if (data.code === 200) {
                    displayStatsCards(data.data);
                }
            } catch (error) {
                showResult('statsResult', { error: error.message }, true);
            }
        }

        // 显示统计卡片
        function displayStatsCards(stats) {
            const container = document.getElementById('statsCards');
            container.innerHTML = `
                <div class="stat-card">
                    <div class="stat-number">${stats.courseCount || 0}</div>
                    <div class="stat-label">总课程数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number">${stats.teacherCount || 0}</div>
                    <div class="stat-label">总教师数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number">${stats.studentCount || 0}</div>
                    <div class="stat-label">总学员数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number">${stats.orderCount || 0}</div>
                    <div class="stat-label">总订单数</div>
                </div>
            `;
        }

        // 2. 获取课程分类统计
        async function getCategoryStats() {
            try {
                const response = await fetch(`${API_BASE}/dashboard/course-category-stats`);
                const data = await response.json();
                showResult('categoryResult', data);
            } catch (error) {
                showResult('categoryResult', { error: error.message }, true);
            }
        }

        // 3. 获取最近7天统计
        async function getRecentStats() {
            try {
                const response = await fetch(`${API_BASE}/dashboard/recent-stats`);
                const data = await response.json();
                showResult('recentResult', data);
            } catch (error) {
                showResult('recentResult', { error: error.message }, true);
            }
        }

        // 4. 获取教师状态统计
        async function getTeacherStatusStats() {
            try {
                const response = await fetch(`${API_BASE}/dashboard/teacher-status-stats`);
                const data = await response.json();
                showResult('teacherStatusResult', data);
            } catch (error) {
                showResult('teacherStatusResult', { error: error.message }, true);
            }
        }

        // 5. 获取课程状态统计
        async function getCourseStatusStats() {
            try {
                const response = await fetch(`${API_BASE}/dashboard/course-status-stats`);
                const data = await response.json();
                showResult('courseStatusResult', data);
            } catch (error) {
                showResult('courseStatusResult', { error: error.message }, true);
            }
        }

        // 6. 测试所有接口
        async function testAll() {
            const results = {};
            
            try {
                // 测试基础统计
                const statsResponse = await fetch(`${API_BASE}/dashboard/stats`);
                results.stats = await statsResponse.json();
                
                // 测试分类统计
                const categoryResponse = await fetch(`${API_BASE}/dashboard/course-category-stats`);
                results.categoryStats = await categoryResponse.json();
                
                // 测试最近7天统计
                const recentResponse = await fetch(`${API_BASE}/dashboard/recent-stats`);
                results.recentStats = await recentResponse.json();
                
                // 测试教师状态统计
                const teacherStatusResponse = await fetch(`${API_BASE}/dashboard/teacher-status-stats`);
                results.teacherStatusStats = await teacherStatusResponse.json();
                
                // 测试课程状态统计
                const courseStatusResponse = await fetch(`${API_BASE}/dashboard/course-status-stats`);
                results.courseStatusStats = await courseStatusResponse.json();
                
                showResult('allTestResult', results);
            } catch (error) {
                showResult('allTestResult', { error: error.message }, true);
            }
        }

        // 页面加载完成后自动获取基础统计
        window.onload = function() {
            getStats();
        };
    </script>
</body>
</html>
